package com.mobi.cacti.ns.view;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.AlertDialog;
import org.holoeverywhere.widget.TextView;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.view.tab.BaseContainerFragment;
import com.mobi.cacti.ns.view.tab.CustInfoTabContainerFragment;
import com.mobi.cacti.ns.view.tab.GraphTabContainerFragment;
import com.mobi.cacti.ns.view.tab.NewsTabContainerFragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;

public class MainActivity extends Activity {

	private static final String CUST_INFO_TAB_TAG = "custinfo";
	private static final String GRAPH_TAB_TAG = "graph";
	private static final String NEWS_TAB_TAG = "news";

	private FragmentTabHost mTabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bottom_tabs);

		ActionBar actionbar = getSupportActionBar();
		if (!actionbar.isShowing())
			actionbar.show();
		actionbar.setDisplayShowHomeEnabled(false);
		actionbar.setDisplayShowTitleEnabled(false);
		actionbar.setDisplayShowCustomEnabled(true);
		actionbar.setCustomView(R.layout.actionbar_layout);

		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		this.addTab(CustInfoTabContainerFragment.class, "Cust Info",
				R.drawable.tab_info_image_selector, CUST_INFO_TAB_TAG);

		this.addTab(GraphTabContainerFragment.class, "Graph",
				R.drawable.tab_graph_image_selector, GRAPH_TAB_TAG);

		this.addTab(NewsTabContainerFragment.class, "News",
				R.drawable.tab_news_image_selector, NEWS_TAB_TAG);
	}

	private void addTab(Class<?> c, String labelId, int drawableId, String tag) {
		TabHost.TabSpec spec = mTabHost.newTabSpec(tag);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, mTabHost.getTabWidget(), false);

		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);

		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);

		int iconHeight = icon.getDrawable().getIntrinsicHeight();
		int titleHeight = title.getLineHeight();
		int padding = 20;
		int tabHeight = iconHeight + titleHeight + padding;

		DisplayMetrics metrics = getResources().getDisplayMetrics();
		// Log.d("test",
		// "dip to px: "+TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
		// 10, metrics));
		// Log.d("test",
		// "px to dip: "+TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
		// 64+title.getLineHeight()+20, metrics));

		ViewGroup.LayoutParams tabIndParamsLayoutPrams = tabIndicator
				.getLayoutParams();

		tabIndParamsLayoutPrams.height = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_PX, tabHeight, metrics);
		tabIndicator.setLayoutParams(tabIndParamsLayoutPrams);

		spec.setIndicator(tabIndicator);

		mTabHost.addTab(spec, c, null);
	}

	@Override
	public void onBackPressed() {
		boolean isPopFragment = false;
		String currentTabTag = mTabHost.getCurrentTabTag();
		if (currentTabTag.equals(CUST_INFO_TAB_TAG)) {
			isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
					.findFragmentByTag(CUST_INFO_TAB_TAG)).popFragment();
		} else if (currentTabTag.equals(GRAPH_TAB_TAG)) {
			isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
					.findFragmentByTag(GRAPH_TAB_TAG)).popFragment();
		} else if (currentTabTag.equals(NEWS_TAB_TAG)) {
			isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
					.findFragmentByTag(NEWS_TAB_TAG)).popFragment();
		}

		if (!isPopFragment) {
			showConfirmDialog();
		}
	}

	private void showConfirmDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Confirm").setMessage("Confirm to Exit TI Monitor ?")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finish();
					}
				}).setNegativeButton("Cancel", null);
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}
}
